/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.transformers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guardianapis.api.client.ItemRequest;
import com.guardianapis.api.client.GuardianConstants;

public class ItemRequestTransformer implements RequestTransformer {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	private Log log = LogFactory.getLog(ItemRequestTransformer.class);
	
	private static final String FORMAT_PARAM = "format";
	
	/*******************************************************************************
	 * Implementing RequestTransformer
	 *******************************************************************************/
	
	public String transformToQueryString(Object pRequest) {
		
		StringBuilder queryStringResult = new StringBuilder();
		if (pRequest instanceof ItemRequest) {
			ItemRequest contentRequest = (ItemRequest) pRequest;
			
			if (contentRequest.getId() != null && !contentRequest.getId().equals("")) {
				queryStringResult.append(GuardianConstants.URL_PATH_SEPERATOR + contentRequest.getId());
			}
			if (contentRequest.getFormat() != null) {
				queryStringResult.append(GuardianConstants.URL_PARAM_INITIALIZER + FORMAT_PARAM + "=" + contentRequest.getFormat().toString().toLowerCase());
			}
		}
		else {
			log.error("Source object is of type: " + pRequest.getClass() + " - expecting ContentRequest");
		}
		
		if (log.isDebugEnabled()) {
			log.debug("Source request: " + pRequest + " was transformed to query string: " + queryStringResult.toString());
		}
		
		return queryStringResult.toString();
	}
	
}
